package levels
{
	import monsters.Downers;
	import monsters.Lefties;
	import monsters.Uppers;
	import objetos.Elevator;
	import objetos.Star;
	import org.flixel.*;
	

	public class Level3 extends Level
	{
		[Embed(source = "../../assets/mapCSV_Level1_Sky.csv", mimeType = "application/octet-stream")] public var skyCSV:Class;
		[Embed(source = "../../assets/mapCSV_Level1_Map.csv", mimeType = "application/octet-stream")] public var mapCSV:Class;
		[Embed(source = "../../assets/mapCSV_Level1_Stars.csv", mimeType = "application/octet-stream")] public var starsCSV:Class;
		[Embed(source = "../../assets/backdrop.png")] public var skyTilesPNG:Class;
		[Embed(source = "../../assets/tiles2.png")] public var mapTilesPNG:Class;
		[Embed(source = "../../assets/disk.png")] public var starPNG:Class;
		
		public function Level3(skipLefties:Boolean = false, skipUppers:Boolean = false, skipDowners:Boolean=false) 
		{
			super();
			
			sky = new FlxTilemap;
			sky.loadMap(new skyCSV, skyTilesPNG, 382, 672);
			sky.setTileProperties(1, FlxObject.NONE);
			sky.scrollFactor.x = 0.9;
			
			map = new FlxTilemap;
			map.loadMap(new mapCSV, mapTilesPNG, 32, 32, 0, 0, 1, 31);
			
			//	Makes these tiles as allowed to be jumped UP through (but collide at all other angles)
			map.setTileProperties(40, FlxObject.UP, null, null, 4);
			
			Registry.map = map;
			Registry.levelExit = new FlxPoint(3 * 32, 16 * 32);
			
			width = map.width;
			height = map.height;
			
			elevator1 = new Elevator(26, 6, 10, 0);
			elevator2 = new Elevator(82, 6, 0, 7);
			
			add(sky);
			add(map);
			add(elevator1);
			add(elevator2);
			
			parseStars();
			
			if (skipDowners == false)
			{
				addDowners();
			}
			
			if (skipLefties == false)
			{
				addLefties();
			}
			
			if (skipUppers == false)
			{
				addUppers();
			}
			
		}
		
		private function addLefties():void
		{
			lefties = new Lefties;
			
			//	The 5 enemy lefties in this level. You could place them in the map editor, then parse the results (as we do with the stars) rather than fixed coordinates here
			lefties.addLefty(11, 16);
			lefties.addLefty(31, 16);
			lefties.addLefty(48, 16);
			lefties.addLefty(80, 16);
			lefties.addLefty(92, 16);
		}
		
		private function addUppers():void
		{
			uppers = new Uppers;
			
			//	The 5 enemy uppers in this level. You could place them in the map editor, then parse the results (as we do with the stars) rather than fixed coordinates here
			uppers.addUpper(13, 17);
			uppers.addUpper(32, 17);
			uppers.addUpper(28, 17);
			uppers.addUpper(89, 17);
		}
		
		private function addDowners():void
		{
			downers = new Downers();
			
			downers.addDowner(22, 4);
			downers.addDowner(91, 4);
		}
		
		override public function change(controle:int):void
		{
			this.lefties.change(controle);
			this.uppers.change(controle);
			this.downers.change(controle);
		}
		
		override public function openExit():void
		{
			//	Removes the two blocking tiles on the right of the map and sets them to nothing, so the player can walk through
			map.setTile(98, 16, 0, true);
			map.setTile(99, 16, 0, true);
		}
		
		private function parseStars():void
		{
			var starMap:FlxTilemap = new FlxTilemap();
			
			starMap.loadMap(new starsCSV, starPNG, 32, 32);
			
			stars = new FlxGroup();
			
			for (var ty:int = 0; ty < starMap.heightInTiles; ty++)
			{
				for (var tx:int = 0; tx < starMap.widthInTiles; tx++)
				{
					if (starMap.getTile(tx, ty) == 1)
					{
						stars.add(new Star(tx, ty));
						totalStars++;
					}
				}
			}
		}
		
	}

}